package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.CategoryInsertParam;
import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.CategoryListVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryTreeVO;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "10.类别管理模块")
@Slf4j
@Validated
public class CategoryController {

    @Autowired
    CategoryService service;
    @PostMapping("/insert")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult insert(@Valid CategoryInsertParam categoryInsertParam){
            service.insert(categoryInsertParam);
            return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除品牌失败，请提交合法的ID值")
                           @RequestParam Long categoryId) {
        service.deleteById(categoryId);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@Valid CategoryUpdateInfoParam updateInfoParam){
        service.update(updateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/updateEnable")
    @ApiOperation("修改类别启用")
    @ApiOperationSupport(order = 310)
    public JsonResult setEnable(Long id){
        service.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/updateDisable")
    @ApiOperation("修改类别禁用")
    @ApiOperationSupport(order = 311)
    public JsonResult setDisable(Long id){
        service.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("/listByParentId")
    @ApiOperation("查询分类")
    @ApiOperationSupport(order = 400)
    public JsonResult list(@RequestParam Long parentId){
        List<CategoryListVO> list = service.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    @GetMapping("/listTree")
    @ApiOperation("查询分类树")
    @ApiOperationSupport(order = 410)
    public JsonResult listTree(){
        List<CategoryTreeVO> categoryTree = service.listTree();
        return JsonResult.ok(categoryTree);
    }

    @GetMapping("/getStandard")
    @ApiOperation("查询分类详情")
    @ApiOperationSupport(order = 420)
    public JsonResult getStandard(Long id){
        CategoryStandardVO standardById = service.getStandardById(id);
        return JsonResult.ok(standardById);
    }
}
